package logic

import (
	"context"
	dictItemApi "mldong/api/dictItem"
	dao "mldong/internal/modules/sys/dao"
	"mldong/internal/modules/sys/entity"
	service "mldong/internal/modules/sys/service"
	"mldong/utility"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/jinzhu/copier"
)

func init() {
	// 注册服务实现
	utility.Register(service.DictItemServiceType, NewDictItemLogic())
}

type DictItemLogic struct {
	dictItemDao *dao.DictItemDao
}

// 保存字典项接口实现
func (logic *DictItemLogic) Save(ctx context.Context, req *dictItemApi.SaveReq) (res bool, err error) {
	model := logic.dictItemDao.Model(nil)
	model = model.Where("dict_id=?", req.DictId)
	unique := utility.CheckUnique(model, "code", req.Code, nil, "唯一编码已存在，请检查code参数")
	if unique != nil {
		return false, unique
	}
	var dictItem = entity.DictItem{}
	err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		req.ID = nil
		// 将req对象赋值给dictItem
		copier.Copy(&dictItem, &req)
		// 插入sys_dictItem表
		dictItem, err = logic.dictItemDao.Insert(dictItem, tx)
		if err != nil {
			return err
		}
		return nil
	})
	return dictItem.ID != nil, err
}

// 修改字典项接口实现
func (logic *DictItemLogic) Update(ctx context.Context, req *dictItemApi.UpdateReq) (res bool, err error) {
	model := logic.dictItemDao.Model(nil)
	model = model.Where("dict_id=?", req.DictId)
	unique := utility.CheckUnique(model, "code", req.Code, req.ID, "唯一编码已存在，请检查code参数")
	if unique != nil {
		return false, unique
	}
	var dictItem = entity.DictItem{}
	var count int64
	// 将req对象赋值给dictItem
	copier.Copy(&dictItem, &req)
	count, err = logic.dictItemDao.UpdateById(dictItem, nil)
	if err != nil {
		return false, err
	}
	return count > 0, err
}

// 删除字典项接口实现
func (logic *DictItemLogic) Remove(ctx context.Context, ids []string) (res bool, _ error) {
	// 遍历ids
	// for _, id := range ids {
	// 	logic.dictItemDao.DeleteById(id)
	// }
	var count, err = logic.dictItemDao.DeleteByIds(ids)
	if err != nil {
		return false, err
	}
	if count == 0 {
		return false, err
	}
	return true, err
}

// 根据ID获取字典项信息
func (logic *DictItemLogic) Detail(ctx context.Context, id string) (res dictItemApi.DictItemVO, err error) {
	var dictItemVO = dictItemApi.DictItemVO{}
	var dictItem entity.DictItem
	dictItem, err = logic.dictItemDao.GetById(id)

	// 将dictItem对象赋值给dictItemVO
	copier.Copy(&dictItemVO, &dictItem)

	return dictItemVO, err
}

// 分页查询字典项接口实现
func (logic *DictItemLogic) Page(ctx context.Context, req *dictItemApi.PageReq) (res dictItemApi.DictItemPageVO, err error) {
	// 处理分页参数nil值
	utility.HandlePageParam(&req.PageParam)
	dictItems, total, err := logic.dictItemDao.Page(*req.PageNum, *req.PageSize, func(model *gdb.Model) *gdb.Model {
		return utility.BuildQuery(req.PageParam, model)
	})
	var dictItemVOS = []dictItemApi.DictItemVO{}
	copier.Copy(&dictItemVOS, &dictItems)
	dictItemPage := dictItemApi.DictItemPageVO{
		Rows: dictItemVOS,
	}
	dictItemPage.PageNum = *req.PageNum
	dictItemPage.PageSize = *req.PageSize
	dictItemPage.RecordCount = total
	// 计算 TotalPage
	if *req.PageSize > 0 {
		dictItemPage.TotalPage = (total + *req.PageSize - 1) / *req.PageSize
	} else {
		dictItemPage.TotalPage = 0 // 如果 PageSize 为 0，则 TotalPage 为 0
	}
	return dictItemPage, err
}
func NewDictItemLogic() service.IDictItemService {
	return &DictItemLogic{
		dictItemDao: dao.NewDictItemDao(),
	}
}
